//<Script language="javascript"/>


function LookupDayOfWeek(dayOfWeek, abbreviate)
{
	var arrDayOfWeek;
	if (abbreviate)
		arrDayOfWeek = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	else
		arrDayOfWeek = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

	return OTGetUserMessage("DateTime", arrDayOfWeek[dayOfWeek])
}

function LookupMonth(month, abbreviate)
{
	var arrMonthNames;
	if (abbreviate)
		arrMonthNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
	else
		arrMonthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

	return OTGetUserMessage("DateTime", arrMonthNames[month])
}

function GetMilitaryTime(strHour, strTimePeriod)
{
	var tmpHour = 0;
	var iHour = parseInt(strHour, 10);
	
	if (strTimePeriod.toUpperCase() == "AM")
	{
		tmpHour = (iHour == 12) ? 0: iHour
	}
	else if (iHour == 12) {tmpHour = iHour}
	else {tmpHour = iHour + 12}
	
	return tmpHour;
}

function GetTimePeriod(strHour)
{	
    return (strHour < 12) ? 'AM' : 'PM'
}

function GetTimePeriodIndex(strHour)
{
	return (strHour < 12) ? 0 : 1;
}

function GetNonMilitaryHour(iHour)
{	
	var tmpHour = parseInt(iHour, 10);
	var h = 1;
	
	if (tmpHour > 12)
	{
		h = tmpHour - 12;
	}
	else
	{
		if (tmpHour == 0)
		{
			h = 12;
		}
		else
		{
			h = tmpHour;
		}
	}
	
	return h;
}

function DB10CharDateToJSDate(datestr) 
{
	//when you have a proc do CONVERT(CHAR(10), somedatefield, 101) to get '08/15/2006' instead of 
	//'08/15/2006 0:00:000' and need to turn it into a javascript date object
	var arrDateParts = datestr.split('/')
	return new Date(arrDateParts[2], arrDateParts[0] - 1, arrDateParts[1]);
}

function JSDateToDB10CharDate(jsdate)
{
	var m = jsdate.getMonth() + 1;
	var y = jsdate.getFullYear();
	var d = jsdate.getDate();
	m = JSPadOut(m);
	d = JSPadOut(d);
	return m + '/' + d + '/' + y;
	
}

function getDateDiffArray(date1, date2, adjustforDST)
{
    if (typeof(adjustforDST) == 'undefined') adjustforDST = false;
    
    var MinMilli = 1000 * 60;
    var HrMilli = MinMilli * 60;
    var DyMilli = HrMilli * 24;
    
    var date1Milli = date1.valueOf()
    var date2Milli = date2.valueOf()
    
    if (adjustforDST == true) {
		//check if we have a daylight savings time issue here
		var date1offset = date1.getTimezoneOffset();
		var date2offset = date2.getTimezoneOffset();
    
		if (date1offset != date2offset) {
			if (date1offset > date2offset) date2Milli += HrMilli
			else if (date1offset < date2offset) date1Milli += HrMilli
		}
    }
    var diff = date2Milli - date1Milli;
    
    var daydiff = Math.floor(Math.abs(diff / DyMilli))
    var dayremainder = (daydiff==0) ? diff : diff % (daydiff * DyMilli)

    var hourdiff = Math.floor(Math.abs(dayremainder / HrMilli))
    var hourremainder = (hourdiff==0) ? dayremainder : dayremainder % (hourdiff * HrMilli)

    var minutediff = Math.floor(Math.abs(hourremainder / MinMilli))
    
    var arrDiffs = new Array(daydiff, hourdiff, minutediff)
    
    return arrDiffs    
}

function otDateAdd(oldDate, days, hours, minutes)
{
    var MinMilli = 1000 * 60;
    var HrMilli = MinMilli * 60;
    var DyMilli = HrMilli * 24;
    var tmpDate = new Date(oldDate)
    
    days = days || 0;
    hours = hours || 0;
    minutes = minutes || 0;
        
    var iDate = tmpDate.valueOf();
    var returndate = new Date(iDate + (parseInt(days, 10) * DyMilli) + (hours * HrMilli) + (parseInt(minutes, 10) * MinMilli))

    return returndate
}

function Y2kYear(theYear)
{
	var intYear = parseInt(theYear);
	if(intYear < 30)
	{
		return intYear + 2000;
	}
	else if (intYear >= 30 && intYear < 100)
	{
		return intYear + 1900;
	}
	
	return intYear;
}

function JSGetDayMonthYearFromStr(dateStr)
{
    var myDate
    var strMyDate = dateStr.replace(/-|\.|\//g, "-");
    var arrDate = strMyDate.split("-");
    
    var userDatePref = window["userDatePreference"] || 0;
    if (userDatePref == OT_DateFormat_MDY_slash || userDatePref == OT_DateFormat_MDY_hyphen || userDatePref == OT_DateFormat_MDY_dot)
    {
		myMonth = arrDate[0];
		myDay = arrDate[1];
		myYear = arrDate[2];
    }

    else if (userDatePref == OT_DateFormat_DMY_slash || userDatePref == OT_DateFormat_DMY_hyphen || userDatePref == OT_DateFormat_DMY_dot)
    {
		myDay = arrDate[0];
		myMonth = arrDate[1];
		myYear = arrDate[2];
    }

    else if (userDatePref == OT_DateFormat_YMD_slash || userDatePref == OT_DateFormat_YMD_hyphen || userDatePref == OT_DateFormat_YMD_dot)
    {
		myYear = arrDate[0];
		myMonth = arrDate[1];
		myDay = arrDate[2];
    }
    return new Array(myDay, myMonth, myYear);
}

function JSUnformatDate(dateStr, dhour, dminute)
{
    // NOTE: dhour and dminute are optional.
    
    var myDate, myYear, myMonth, myDay
    var arrDate = JSGetDayMonthYearFromStr(dateStr);
    
    if (typeof(dhour) == 'undefined') dhour = 0;
    if (typeof(dminute) == 'undefined') dminute = 0;
    
    myDay = arrDate[0];
	myMonth = arrDate[1];
	myYear = arrDate[2];
        
    var newdate = new Date(Y2kYear(myYear), myMonth-1, myDay, dhour, dminute)
    return newdate
    
}

// A JavaScript version of OT_FormatDate
// Takes valid Javascript date elements and returns a date in the user's preferred format
// Note: use getFullYear() in JS to return a 4-character year value

function JSFormatDate(y,m,d)
{
	if (JSFormatDate.arguments.length == 1 && typeof(JSFormatDate.arguments[0]) == 'object')
	{
		//ok, instead of making a new version of this function, I'm taking the current one
		//and adapting it to work with a normal javascript date.
		
		m = JSFormatDate.arguments[0].getMonth() + 1;
		d = JSFormatDate.arguments[0].getDate();
		y = JSFormatDate.arguments[0].getFullYear();	
	}

	m = JSPadOut(m)
	d = JSPadOut(d)
	var datepref;
	var tmpDate;
	datepref = window["userDatePreference"] || 0;

	switch (datepref)
	{
		case OT_DateFormat_MDY_slash:
			tmpDate = m + "/" + d + "/" + y;
			break;
		case OT_DateFormat_MDY_hyphen:
			tmpDate = m + "-" + d + "-" + y;
			break;
		case OT_DateFormat_MDY_dot:
			tmpDate = m + "." + d + "." + y;
			break;
		case OT_DateFormat_DMY_slash:
			tmpDate = d + "/" + m + "/" + y;
			break;
		case OT_DateFormat_DMY_dot:
			tmpDate = d + "." + m + "." + y;
			break;			
		case OT_DateFormat_DMY_hyphen:
			tmpDate = d + "-" + m + "-" + y;
			break;			
		case OT_DateFormat_YMD_slash:
			tmpDate = y + "/" + m + "/" + d;
			break;				
		case OT_DateFormat_YMD_dot:
			tmpDate = y + "." + m + "." + d;
			break;			
		case OT_DateFormat_YMD_hyphen:
			tmpDate = y + "-" + m + "-" + d;
			break;
		default:
			tmpDate = m + "/" + d + "/" + y;
		}
	return tmpDate
}

function OT_FormatDate(strDate) {
	//formats a date object or date string for a user (like 2008-02-07 16:47
	if (!strDate) return '';
	if (strDate.constructor == String)
	{
		strDate = new Date(strDate.replace(/-/g, "\/"));
	}
	return JSFormatDate(strDate);
}

function formatVBDate(strDate, bTime)
{
    var tmpDate = JSPadOut(strDate.getMonth() +1) + '/' + JSPadOut(strDate.getDate()) + '/' + strDate.getFullYear();
    if (bTime) tmpDate += ' ' + JSPadOut(strDate.getHours()) + ':' + JSPadOut(strDate.getMinutes());
    return tmpDate
}


function formatIsoDate(strDate, bTime)
{
    var tmpDate = strDate.getFullYear() + '-' + JSPadOut(strDate.getMonth() +1) + '-' + JSPadOut(strDate.getDate());
    if (bTime) tmpDate += ' ' + JSPadOut(strDate.getHours()) + ':' + JSPadOut(strDate.getMinutes());
    return tmpDate
}


function formatDayOfWeekMonthDay(datetime)
{
	var dow = datetime.getDay();
	var output = LookupDayOfWeek(dow, true);
	output += ',&nbsp;' + formatMonthDay(datetime)
	return output;
}

function formatMonthDay(datetime)
{
	var month = datetime.getMonth();
	var dayOfWeek = datetime.getDay();

	var dp = (window['userDatePreference'] || 0)
	switch (dp)
	{
		case OT_DateFormat_MDY_slash:
		case OT_DateFormat_MDY_hyphen:
		case OT_DateFormat_MDY_dot:
		case OT_DateFormat_YMD_slash:
		case OT_DateFormat_YMD_hyphen:
		case OT_DateFormat_YMD_dot:
			return LookupMonth(month, true) + ' ' + datetime.getDate();
			break;
		default:
			return datetime.getDate() + ' ' + LookupMonth(month, true);
			break;
	}
}


function jsFormatTime(iHour, iMinute, sPeriod)
{
    var tmpTime
    
    tmpTime = JSPadOut(iHour) + ':' + JSPadOut(iMinute);
    if (typeof(sPeriod) != 'undefined') tmpTime+= ' ' + sPeriod;
    return tmpTime

}

// Takes a military time in format hh:mm
// userTimePref & user24HrPref can be passed in as null because of the
// global userTimePreference and userMilitaryTime set in inc_pageTemplate.asp
// spacePadHour helps times line up in columns by adding nbsps to single-char hours
function JSLocalizeTime(strTime, userTimePref, user24HrPref, lowerTimePeriod, spacePadHour, forceSpaceAmPm)
{
    var tmpTime, timeArr, timeSep, tmpHour
    var timePeriod = "";
    
    if (userTimePref == null && window['userTimePreference'] != null)
		userTimePref = window.userTimePreference; //global variable

    if (user24HrPref == null && window['userMilitaryTime'] != null)
		user24HrPref = window.userMilitaryTime; //global variable
		
	if (strTime.toLocaleTimeString) //test to see if its a date object
	{
		//turn it into a time string
		strTime = strTime.getHours() + ':' + JSPadOut(strTime.getMinutes());
	}
    
    switch(parseInt(userTimePref, 10))
    {
		case 0:
			timeSep = ":";
			break;
		case 1:
			timeSep = ".";
			break;
		case 2:
			timeSep = ",";
			break;
		default:
			timeSep = ":";
    }
    
    timeArr = strTime.split(":");
    
    tmpTime = strTime;
    if (user24HrPref == "False" || user24HrPref == 0|| user24HrPref == false)
    {
		timePeriod = GetTimePeriod(timeArr[0]);
		tmpHour = GetNonMilitaryHour(timeArr[0]);
		if (spacePadHour && tmpHour.toString().length == 1) {
		 tmpHour = '&nbsp;&nbsp;' + tmpHour;
		}
		var strSpace = (forceSpaceAmPm) ? ' ' : '';
		tmpTime = (lowerTimePeriod) ? tmpHour + timeSep + timeArr[1] + strSpace + timePeriod.toLowerCase() : tmpHour + timeSep + timeArr[1] + " " + timePeriod;
    }
    else
    {
		tmpTime = JSPadOut(parseInt(timeArr[0], 10)) + timeSep + JSPadOut(parseInt(timeArr[1], 10));
    }
    
    return tmpTime;
}

function OT_FormatTime(strDate)
{
	//takes a date object or date string, and returns user time format
	if (!strDate) return '';
	if (strDate.constructor == String)
	{
		strDate = new Date(strDate.replace(/-/g, "\/"));
	}

	return JSLocalizeTime(strDate);
}

function OT_FormatDateTime(strDate)
{
	//takes a date object or date string, and returns user time format
	if (!strDate) return '';
	return OT_FormatDate(strDate) + ' ' + OT_FormatTime(strDate);
}

function JSDateAdd(interval, amount, theDate)
// Takes & returns a javascript date object
// You must handle (un)localizing outside of this function
{
	var dayNum = theDate.getDate();
	var monthNum = theDate.getMonth();
	var yearNum = theDate.getFullYear();		
	switch (interval)
	{
		case "d":
			dayNum = dayNum + amount;
			break;
		case "m":
			monthNum = monthNum + amount;
			break;
		case "y":
			yearNum = yearNum + amount;
			break;
	}
	return new Date(yearNum, monthNum, dayNum);
}	

function compareDateFieldsAndAdd(interval, amount, dateField1, dateField2)
{
	// Adds increment + amount to dateField2 if it's less than dateField1
	// CheckDate does unlocalizing of dates; returns localized value
	if (dateField1.value == "") return;
	if (checkDate(dateField1, ""))
	{
		var date1 = new Date(longYear, parseInt(userMonth, 10) - 1, userDay);
		var returnDate = JSDateAdd(interval, amount, date1)
		if (dateField2.value == "") 
		{
			dateField2.value = JSFormatDate(returnDate.getFullYear(), returnDate.getMonth() + 1, returnDate.getDate());
		}
		else if (checkDate(dateField2, ""))
		{
			var date2 = new Date(longYear, parseInt(userMonth, 10) - 1, userDay);
			if (date2.getTime() >= date1.getTime())
			{
				return;
			}
			else
			{
				dateField2.value = JSFormatDate(returnDate.getFullYear(), returnDate.getMonth() + 1, returnDate.getDate());
			}
		}
	}
}
